from app import ma
from app.auth.models import User

# 简化版用户 Schema（供其他模块嵌套）
class UserSimpleSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = User
        load_instance = True
        fields = ('id', 'username', 'avatar')

user_simple_schema = UserSimpleSchema()

# 用户详情 Schema（若嵌套 CommentSchema，用字符串引用）
class UserProfileSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = User
        load_instance = True
        fields = ('id', 'username', 'email', 'avatar', 'created_at', 'comments')
    
    # 【关键修复】跨模块嵌套用字符串引用
    comments = ma.Nested('app.comments.schemas.CommentSchema', many=True, exclude=('user',))

user_profile_schema = UserProfileSchema()

# 包含食谱的用户 Schema
class UserWithRecipesSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = User
        load_instance = True
        fields = ('id', 'username', 'email', 'avatar', 'created_at', 'comments', 'recipes')
    
    # 【关键修复】跨模块嵌套用字符串引用
    comments = ma.Nested('app.comments.schemas.CommentSchema', many=True, exclude=('user',))
    recipes = ma.Nested('app.recipes.schemas.RecipeSimpleSchema', many=True, exclude=('user',))

user_with_recipes_schema = UserWithRecipesSchema()

# 多个用户的 Schema
users_schema = UserSimpleSchema(many=True)